import { defineStore } from "pinia"
import piniaPluginPersist from "pinia-plugin-persist"
import Product from "@/api/main"
import { Toast } from "vant"
export const useMainStore = defineStore('main', {
    state: () => {
        return {
            info: 'pinia 可以使用',
            count: 0,
            cart: [
                {
                    cailiao_id: 1,
                    goods_id: 1,
                    goods_title: '王爽老师【999纯银泥石纹开口戒指成品定制】',
                    goods_type: '戒指',
                    goods_price: 238.00,
                    goods_num: 1,
                    goods_img: 'https://img.zcool.cn/community/01b6b85bcd3e1ea801213dea5b7321.jpg@1280w_1l_2o_100sh.jpg',
                    is_check: false
                }
            ],
            myAddress: [
                {
                    id: '0',
                    name: '朱晓亮',
                    tel: 18236956686,
                    province: '河南省',
                    addressDetail: '河南省郑州市管城回族自治区666号黄河科技学院',
                    city: '郑州市',
                    county: '管城回族自治区',
                    isDefault: true
                }
            ]
        }
    },
    getters: {
        //函数选择接受一个参数 state状态对象
        //商品总计
        allPrice() {
            let allPrice = 0
            this.cart.filter(item => {
                return item.is_check == true
            }).forEach(item => {
                allPrice += item.goods_num * item.goods_price
            })
            return allPrice
        },
        //购物车是否全选
        isAllChecked(): any {

            let noCheck = this.cart.filter(item => {
                return item.is_check == false
            })

            let isAllChecked = noCheck.length == 0 ? true : false
            if (this.cart.length == 0) isAllChecked = false
            return isAllChecked
        }
    },
    actions: {
        //actions 里面的函数不要写箭头函数要不this指向有问题
        //支持异步
        changeAwait() {
            setTimeout(() => {
                this.count += 1000
            }, 1000);
        },
        //购物车全选事件
        all_check(event: any) {
            this.cart.forEach(item => {
                item.is_check = event
            })
        },
        //结算
        jiesuan(cc: any) {
            let no_select = this.cart.filter(item => {
                return item.is_check == false
            })
            let select = this.cart.filter(item => {
                return item.is_check == true
            })

            if (no_select.length == this.cart.length) {
                Toast({
                    duration: 3000,
                    message: '至少选择一个宝贝'
                })
            } else {
                if (cc == 1) {
                    return select
                } else {
                    this.cart = this.cart.filter(item => {
                        return item.is_check == false
                    })
                    Toast.success({
                        duration: 3000,
                        message: '删除成功'
                    })
                }
            }

        },
        //修改默认地址
        change_address(cc: any) {
            console.log(cc);
            this.myAddress.forEach((item => {
                item.isDefault = false
                if (item.id == cc) {
                    item.isDefault = true
                }
            }))
        }
    },
    persist: {
        enabled: true,
        // strategies: [
        //     {
        //         //自定义名
        //         key: 'xxx',
        //         //选择储存位置 不指定默认在sessionStorage
        //         storage: localStorage,
        //         //配置白名单（只有在数组里面的才会被记录）
        //         paths: ['count']
        //     }
        // ]
    }
})